<template>
  <div class="app-wrapper">
    <headerTitle
      :titleList="titleList"
    />
    <div class="app-container"  ref="block">
      <div class="containerMain">
        <div class="search-item">
          <div class="item-list">
            <div class="item-con">
              <span>日期</span>
              <el-date-picker
                  v-model="timeValue"
                  unlink-panels
                  size="mini"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['00:00:00', '23:59:59']"
                >
                </el-date-picker>
            </div>
            <div class="item-con">
              <span>客户姓名</span>
              <el-input
                style="width: 200px"
                size="mini"
                v-model="searchform.agentId"
                placeholder="请输入坐席ID"
              ></el-input>
            </div>
            <div class="item-con">
              <span>性别</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.agentId"
              >
                <el-option label="全部" value="0"></el-option>
                <el-option label="男" value="1"></el-option>
                <el-option label="女" value="2"></el-option>
              </el-select>
            </div>
             <div class="item-con">
              <span>手机号</span>
              <el-input
                style="width: 200px"
                size="mini"
                v-model="searchform.agentId"
                placeholder="请输入手机号"
              ></el-input>
            </div>
            <div class="item-con">
              <span>客户类型</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.agentId"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="vip" value="1"></el-option>
                <el-option label="普通" value="2"></el-option>
              </el-select>
            </div>
          </div>
          <div class="item-btn">
            <el-button
              class="kyeup_search_btn"
              type="primary"
              size="mini"
              @click="() => fetchData()"
              >搜索</el-button
            >
            <el-button type="default" size="mini" @click="() => reset()"
              >重置</el-button
            >
          </div>
        </div>
        <div class="container">
          <div class="div-table">
            <el-table
              :header-cell-style="headerStyle"
              :data="list"
              element-loading-text="Loading"
              fit
              size="mini"
              highlight-current-row
              :height="tableHeight"
            >
              <el-table-column label="创建日期">
                <template slot-scope="scope">{{ scope.row.id }}</template>
              </el-table-column>
              <el-table-column label="客户名称">
                <template slot-scope="scope">{{
                  scope.row.agentName
                }}</template>
              </el-table-column>
              <el-table-column label="性别">
                <template slot-scope="scope">{{ scope.row.agentVideoNum }}</template>
              </el-table-column>
              <el-table-column label="业务类型">
                <template slot-scope="scope">{{
                  scope.row.skillName
                }}</template>
              </el-table-column>

              <el-table-column label="手机号">
                <template slot-scope="scope">{{
                  scope.row.callNumTimes
                }}</template>
              </el-table-column>
              <el-table-column label="身份证号">
                <template slot-scope="scope">{{
                  scope.row.callTotalDurationStr
                }}</template>
              </el-table-column>
              <el-table-column label="居住地址">
                <template slot-scope="scope">{{
                  scope.row.callAvgDurationStr
                }}</template>
              </el-table-column>
              <el-table-column label="客户类型" :formatter="formatter">
                <template slot-scope="scope">{{
                  scope.row.callAvgDurationStr
                }}</template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <pagination
      :exportShow="false"
      :type="'primary'"
      @handelExport="exportDetail"
      btnExport="导出"
      :pagination="pagination"
    />


  </div>
</template>

<script>

import moment from "moment";
import Pagination from "@/components/Pagination/index";
import HeaderTitle from "@/components/Headertitle/index";
import {
  fetchAgentList
} from "@/api/systemapi/agent";
import { fetchSeatPerformList, fetchSeatExport, fetchAllSkill,checkSeatExport,fetchAttendeeDetail } from "@/api/systemapi/video";//搜索  导出
import { pageParams } from "@/settings";

export default {
  components: {
    Pagination,
    HeaderTitle
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: "启用",
        2: "禁用",
      };
      return statusMap[status];
    },
  },
  data() {
    const TODAYSTART = moment().startOf('day').format('YYYY-MM-DD 00:00:00')
    const TODAYEND = moment().endOf('day').format('YYYY-MM-DD 23:59:59')
    return {
      titleList: [{ title: "客户管理" }],
      list:[],
      tableHeight: "300", //表格默认高度
      timeValue: [TODAYSTART, TODAYEND],
      listLoading: true,
      skillGroupList: [],
      searchform: {
        rows: "10",
        page: "1",
        startTime:"",
        endTime:"",
        agentId:"",
        agentName:"",
        agentVideoNum:"",//坐席视讯号
        skillId:"",//技能组ID
      },
      // 分页
      pagination: pageParams
    };
  },
  watch: {
    // 分页
    "pagination.currentPage": function () {
      this.searchform.page = this.pagination.currentPage;
      this.fetchData();
    },
    "pagination.pageSize": function () {
      this.searchform.rows = this.pagination.pageSize;
      this.searchform.page = 1;
      this.fetchData();
    },
  },
  mounted() {
    this.$nextTick(() => {
      let self = this;
      this.tableHeight = this.$refs.block.offsetHeight - 220;
      window.onresize = () => {
        return (() => {
          this.tableHeight = this.$refs.block.offsetHeight - 220;
        })();
      };
    });
    this.fetchSkillList();
    this.fetchData()
  },
  methods: {
    //表头背景颜色修改
    headerStyle({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:#F5F6FB;color:#666";
      }
    },
    // 重置搜索，刷新列表
    reset() {
      const TODAYSTART = moment().startOf('day').format('YYYY-MM-DD 00:00:00')
      const TODAYEND = moment().endOf('day').format('YYYY-MM-DD 23:59:59')
      this.timeValue = [TODAYSTART, TODAYEND],
      this.searchform = {
        startTime:this.timeValue[0],
        endTime:this.timeValue[1],
        agentId:"",
        agentName:"",
        agentVideoNum:"",//坐席视讯号
        skillId:"",//技能组ID
        rows: "10",
        page: "1",
      }
    },
    fetchSkillList() {//获取技能组ID
      fetchAllSkill({}).then((response) => {
        if (response.code == 0) {
          console.log(response,"技能组")
        } else {
          this.$message({
            type: "error",
            message: response.msg,
          });
        }
      });
    },
    formatter(row, column) {
        return row.userType==1 ? "坐席" : "客户";
    },
    exportDetail() {//导出
        if (this.timeValue != null && this.timeValue.length > 0) {
          this.searchform.startTime = moment(this.timeValue[0]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          this.searchform.endTime = moment(this.timeValue[1]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      checkSeatExport({...this.searchform}).then(res=>{//导出验证
        console.log(res,"daochuyanzhneg")
        if(res.code == 0){
          fetchSeatExport({ ...this.searchform }).then((res) => {
            console.log(res,"daochudaochu");
            let blob = new Blob([res.data], {
              type: "application/vnd.ms-excel",
            });
            console.log(res.headers["content-disposition"],"res.headers[")
            let str = res.headers["content-disposition"].split(";")[1];
            
            // console.log(str,"str1111")
            if (str.indexOf("=") != -1) {
              var filename = decodeURI(str.slice(9));
            } else {
              var filename = decodeURI(str);
            }
            var downloadElement = document.createElement("a");
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = filename; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          });
        }else{
          this.$message({
            type: "error",
            message: res.msg,
          });
        }
      })
      
    },

    fetchData() {//搜索
      console.log(this.timeValue,"this.timeValue");
      if (this.timeValue != null && this.timeValue.length > 0) {
          this.searchform.startTime = moment(this.timeValue[0]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          this.searchform.endTime = moment(this.timeValue[1]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        console.log(this.searchform.startTime,"231232312");

      // fetchSeatPerformList({...this.searchform}).then((response) => {
      //   if (response.code == 0) {
      //     this.list = response.data;
      //     this.pagination.total = response.data.total;
      //   } else {
      //     this.$message({
      //       type: "error",
      //       message: response.msg,
      //     });
      //   }
      // });
    },
  },
};
</script>
<style lang="scss">
.tree-con {
  .el-tree-node__content {
    margin-bottom: 20px;
  }
}
</style>
<style lang="scss" scoped>
.containerMain {
  .search-item {
    padding: 20px 10px;
    font-size: 14px;
    color: #666;
    display: flex;
    justify-content: space-between;
    .item-list {
      display: flex;
      flex-wrap: wrap;
    }
    .item-con {
      display: flex;
      margin-right: 30px;
      margin-top: 20px;

      span {
        display: inline-block;
        line-height: 28px;
        margin-right: 10px;
      }
    }
    .item {
      width: 180px;
    }
  }
  .container {
    display: flex;
    flex-direction: row;
    .div-tree {
      width: 250px;
    }
    .div-table {
      flex: 1;
      width: 100%;
      overflow: hidden;
      .el-table {
        height: 100%;
        width: 100%;
        overflow: auto;
      }
    }
    .el-row {
      height: 300px;
      .el-col {
        height: 100%;
        .div-tree {
          height: 90%;
          .el-tree {
            height: 100%;
            overflow-y: auto;
          }
        }
      }
    }
  }
  .item-btn {
    margin-top: 20px;
    flex: 0 0 140px;
  }
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
